<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\CouponModel;
use Model\SettingModel;

/**
 * 卡券
 * Class Enter
 * @package Controller
 */
class Coupon extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加编辑卡券
     * @param array $_data 数据
     * @param int $_coupon_id 卡券id
     * @return mixed
     */
    public function edit(Array $_data,$_coupon_id = 0,$_update_status = true)
    {
        $_coupon_check = SettingModel::where("uniacid",$this->_uniacid)->value("coupon_check");
        if($_coupon_id) {
            if($_coupon_check && $_update_status) $_data["status"] = 2;
            return CouponModel::where("id",$_coupon_id)->update($_data);
        } else {
            $_data["uniacid"] = $this->_uniacid;
            $_data["uid"] = $this->_uid;
            $_data["status"] = $_coupon_check == 1
                ? 2 : 1;
            $_coupon_id = CouponModel::createId($_data);
        }
        return $_coupon_id;
    }

    /**
     * 获取用户卡券列表
     * @param array $_limit
     * @param int $_uid
     * @param string $_type
     * @return CouponModel
     */
    public function getUserAll(Array $_limit,$_uid = 0,$_type = 'qb')
    {
        if(!$_uid) $_uid = $this->_uid;
        $CouponModel = CouponModel::where("uniacid",$this->_uniacid)
                                    ->where("uid",$_uid)
                                    ->where("status","!=",9)
                                    ->limit($_limit[0],$_limit[1])
                                    ->order("status = 2 desc")
                                    ->order("id","desc")
                                    ->cache(!KUYUAN_DEBUG);
        if($_type != 'qb') $CouponModel->where("type",$_type);
        return $CouponModel->select();
    }

    /**
     * 获取一个卡券信息
     * @param $_coupon_id
     * @return CouponModel
     */
    public function get($_coupon_id)
    {
        return CouponModel::where("id",$_coupon_id)
                    ->where("status","!=",9)
                    ->cache(!KUYUAN_DEBUG)
                    ->find();
    }

    /**
     * 获取卡券的原生数据
     * @param $_coupon_id
     * @return CouponModel
     */
    public function take($_coupon_id)
    {
        return CouponModel::where("id",$_coupon_id)
            ->where("status","!=",9)
            ->cache(!KUYUAN_DEBUG)
            ->noGetAttr("cover,time_str")
            ->find();
    }

    /**
     * 删除卡券
     * @param $_coupon_id
     * @return mixed
     */
    public function del($_coupon_id)
    {
        return CouponModel::where("id",$_coupon_id)
                            ->delete();
    }

    /**
     * 获取指定字段数据
     * @param string $_field 字段
     * @param int $_coupon_id 卡券id
     * @return mixed
     */
    public function value($_field,$_coupon_id)
    {
        return CouponModel::where("id",$_coupon_id)
            ->cache(!KUYUAN_DEBUG)
            ->value($_field);
    }

    /**
     * 获取需要审核的卡券列表
     * @param array $_limit
     * @return mixed
     */
    public function getCheckList(Array $_limit)
    {
        $_list = CouponModel::where("uniacid",$this->_uniacid)
            ->where("status",2)
            ->order("id","desc")
            ->limit($_limit[0],$_limit[1])
            ->select();
        if($_list) {
            $Member = new Member();
            foreach($_list as $_k=>$_v) {
                $_list[$_k]["user"] = $Member->findNameHeader($_v["uid"]);
            }
        }
        return $_list;
    }

    /**
     * 获取需要审核的卡券总数
     */
    public function getCheckTotal()
    {
        return CouponModel::where("uniacid",$this->_uniacid)
            ->where("status",2)
            ->count();
    }
}